package com.yfp.client.config;

import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.google.common.collect.Maps;
import jakarta.annotation.PostConstruct;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: 微信配置
 *
 * @CreateDate: 2024/5/16 11:57
 * @UpdateUser:
 * @UpdateDate: 2024/5/16 11:57
 * @UpdateRemark: 暂无修改
 * @Version: 1.0
 */
@Configuration
@ConditionalOnClass(WxPayService.class)
@EnableConfigurationProperties(WxPayProperties.class)
@AllArgsConstructor
public class WxPayConfiguration {
    private WxPayProperties properties;

    private static Map<String, WxPayService> wxPayServices = Maps.newHashMap();

    public static WxPayService getMxPayService(WxPayProperties.Config config) {
        String appIdAndmchId = config.getAppId() + "-" + config.getMchId();
        WxPayService wxPayService = wxPayServices.get(appIdAndmchId);
        if (wxPayService == null) {
            throw new IllegalArgumentException(String.format("未找到对应mchId=[%s]的配置，请核实！", appIdAndmchId));
        }
        return wxPayService;
    }

    @PostConstruct
    public void init() {
        List<WxPayProperties.Config> configs = this.properties.getConfigs();
        if (configs == null) {
            throw new RuntimeException("大哥，拜托先看下项目首页的说明（readme文件），添加下相关配置，注意别配错了！");
        }
        wxPayServices = configs.stream()
                .map(a -> {
                    WxPayConfig payConfig = new WxPayConfig();
                    payConfig.setAppId(StringUtils.trimToNull(a.getAppId()));
                    payConfig.setMchId(StringUtils.trimToNull(a.getMchId()));
                    payConfig.setMchKey(StringUtils.trimToNull(a.getMchKey()));
                    payConfig.setSubAppId(StringUtils.trimToNull(a.getSubAppId()));
                    payConfig.setSubMchId(StringUtils.trimToNull(a.getSubMchId()));
                    payConfig.setKeyPath(StringUtils.trimToNull(a.getKeyPath()));
                    payConfig.setApiV3Key(StringUtils.trimToNull(a.getApiV3Key()));
                    payConfig.setPrivateKeyPath(StringUtils.trimToNull(a.getPrivateKeyPath()));
                    payConfig.setPrivateCertPath(StringUtils.trimToNull(a.getPrivateCertPath()));
                    // 可以指定是否使用沙箱环境
                    payConfig.setUseSandboxEnv(false);

                    WxPayService wxPayService = new WxPayServiceImpl();
                    wxPayService.setConfig(payConfig);
                    return wxPayService;

                }).collect(Collectors.toMap(s -> s.getConfig().getAppId() + "-" + s.getConfig().getMchId(), a -> a));
    }

    @Bean
    @ConditionalOnMissingBean
    public WxPayService wxService() {
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(StringUtils.trimToNull(this.properties.getConfigs().get(0).getAppId()));
        payConfig.setMchId(StringUtils.trimToNull(this.properties.getConfigs().get(0).getMchId()));
        payConfig.setMchKey(StringUtils.trimToNull(this.properties.getConfigs().get(0).getMchKey()));
        payConfig.setSubAppId(StringUtils.trimToNull(this.properties.getConfigs().get(0).getSubAppId()));
        payConfig.setSubMchId(StringUtils.trimToNull(this.properties.getConfigs().get(0).getSubMchId()));
        payConfig.setKeyPath(StringUtils.trimToNull(this.properties.getConfigs().get(0).getKeyPath()));
        payConfig.setApiV3Key(StringUtils.trimToNull(this.properties.getConfigs().get(0).getApiV3Key()));
        payConfig.setPrivateKeyPath(StringUtils.trimToNull(this.properties.getConfigs().get(0).getPrivateKeyPath()));
        payConfig.setPrivateCertPath(StringUtils.trimToNull(this.properties.getConfigs().get(0).getPrivateCertPath()));
        // 可以指定是否使用沙箱环境
        payConfig.setUseSandboxEnv(false);

        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(payConfig);
        return wxPayService;
    }
}
